package guild

import (
	"context"
	"fmt"
	"github.com/gogf/gf/v2/os/gmlock"
	"xr-game-server/core/constants/common"
	"xr-game-server/core/httpserver"
	"xr-game-server/dao/guilddao"
	"xr-game-server/dao/roledao"
	"xr-game-server/dto/guilddto"
	"xr-game-server/entity"
	"xr-game-server/errercode"
)

// 设置工会自动审批通过
func SetGuildAutoApprove(ctx context.Context, reqTemp *guilddto.SetGuildAutoApproveReq) (bool, error) {
	roleId := httpserver.GetAuthId(ctx)
	roleData := roledao.GetRoleByRole(roleId)
	if roleData.GuildId == common.Zero {
		return false, errercode.CreateCode(errercode.GuildNonExist)
	}
	lockKey := fmt.Sprintf(GuildLock, roleData.GuildId)
	defer gmlock.Unlock(lockKey)
	lockFlag := gmlock.TryLock(lockKey)
	if !lockFlag {
		return false, errercode.CreateCode(errercode.SysBuyLimit)
	}
	memberCache := guilddao.GetMemberByRoleId(roleData.GuildId, roleId)
	if memberCache.Title == entity.GuildMemberTitleMember {
		return false, errercode.CreateCode(errercode.NoPermission)
	}
	guildData := guilddao.GetGuild(roleData.GuildId)
	guildData.SetAutoPass(reqTemp.Status)
	return true, nil
}
